package fucai.com.fucai.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fucai.com.fucai.R;
import fucai.com.fucai.blmanager.DataManager;
import fucai.com.fucai.db.FuCaiNumber;

/**
 * A simple {@link Fragment} subclass.
 */
public class ToolsFragment extends Fragment {

    private DataManager mDataManager = null;
    private View mView = null;
    @ViewInject(R.id.tv)
    private TextView tv;
    @ViewInject(R.id.look)
    private Button look;
    private List<FuCaiNumber> mFucaiNumber = null;
    @ViewInject(R.id.lv)
    private ListView lv;
    private List<Map<String, String>> maps;
    private SimpleAdapter adapter;

    public ToolsFragment() {
        // Required empty public constructor
    }

    public static ToolsFragment getInstance() {
        return new ToolsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_tools, container, false);
        initView();
        return mView;
    }

    private void initView() {
        ViewUtils.inject(this, mView);
        mFucaiNumber = new ArrayList<>();
        mDataManager = new DataManager(getActivity());
        maps = new ArrayList<>();
        look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFucaiNumber.clear();
                maps.clear();
                // mFucaiNumber = mDataManager.findAll();
                mFucaiNumber = mDataManager.findLime(2);
                //   Collections.reverse(mFucaiNumber);
                for (int i = 0; i < mFucaiNumber.size(); i++) {
                    Map<String, String> map = new HashMap<>();
                    FuCaiNumber fu = mFucaiNumber.get(i);
                    map.put("issue", "第" + fu.getIssue() + "期");
                    map.put("time", mFucaiNumber.get(i).getTime());
                    map.put("open", "开奖号: " + fu.getOpenBaiWei() + "" + fu.getOpenShiwei() + "" + fu.getOpenGewei());
                    map.put("test", "试机号：" + fu.getTestBaiWei() + "" + fu.getTestShiwei() + "" + fu.getTestGewei());
                    maps.add(map);
                }
                adapter.notifyDataSetChanged();
            }
        });
        setDate();
    }

    private void setDate() {
        adapter = new SimpleAdapter(getActivity(),
                maps,
                R.layout.list_item,
                new String[]{"issue", "time", "open", "test"},
                new int[]{R.id.issue, R.id.time, R.id.open, R.id.test}
        );
        lv.setAdapter(adapter);
    }
}
